﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.Models.ICM.ViewModel;
using KPIS.GERP.Models.SYS;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.ServiceModel;
using System.Web.Mvc;

namespace KPIS.GERP.WCF.GIMS
{
    [ServiceContract]
    public interface IMasUnitService
    {
        [OperationContract]
        IEnumerable<KPIS.GERP.Models.ICM.MasUnit> SelectMasUnit(MasUnit criteriaModel = null, List<string> includeSubObjectString = null);
    }

    [ServiceErrorBehavior(typeof(ElmahErrorHandler))]
    public class MasUnitService : IMasUnitService
    {
        public IEnumerable<KPIS.GERP.Models.ICM.MasUnit> SelectMasUnit(MasUnit criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasUnit> query = context.MasUnits;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
